define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefined, Backend, Form, Table) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'resource/index' + location.search,
                    add_url: 'resource/add',
                    edit_url: 'resource/edit',
                    del_url: 'resource/del',
                    multi_url: 'resource/multi',
                    table: 'resource'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
				searchFormVisible: true,
				columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: __('Id'), searchable: false},
						{field: 'industry', title: __('行业'), operate: 'in', formatter: Table.api.formatter.label, searchList: Config.industryList},
                        {field: 'category', title: __('类别'), operate: 'in', formatter: Table.api.formatter.label, searchList: Config.categoryList},
                        {field: 'content', title: __('内容'), sortable: true, operate: 'like', align:'left', width:'60%', cellStyle:function(value, row, index){
							return {css:{"white-space": "normal"}};
						}},
                        {field: 'admin_id', title: __('录入人'), addClass: "selectpage", extend: "data-source='auth/admin/index' data-field='nickname'", formatter:function(value, row, index){
							return row.admin.nickname;
						}},
						{field: 'createtime', title: __('Createtime'), width: 120, formatter: Table.api.formatter.datetime, datetimeFormat: 'YYYY-MM-DD', operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
			
			
			// 导入
            $(document).on('click', '.btn-imports', function () {
               Layer.open({
                    title: __('Import'),
                    content: Template("typetpl", {}),
                    btn: [__('OK'), __('CANCEL')],
                    yes: function (index, layero) {
						var industry = $("select[name='industry']", layero).val();
                        var category = $("select[name='category']", layero).val();
						var file = $("input[name='file']", layero).val();
                        Fast.api.ajax({
                            url: "resource/import",
                            type: "post",
                            data: {industry: industry, category: category, file: file},
                        }, function () {
                            table.bootstrapTable('refresh', {});
                            Layer.close(index);
                        });
                    }
                });
				
				Form.api.bindevent($("form[role=form]"));
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
				$(document).on('click','button[type="reset"]', function(){
					var index = parent.Layer.getFrameIndex(window.name);
					parent.Layer.close(index);
				});
			
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
